#include <QSplitter>
#include <QWidget>
#include <QBoxLayout>
#include <QToolButton>

#include "workspace.h"
#include "mainmenu.h"
#include "toolbutton.h"
#include "maintoolbar.h"
#include "dockstack.h"

#include "sidebutton.h"

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    m_mainMenu(new MainMenu(this)),
    m_workspace(new Workspace(this)),
    m_mainToolBar(new MainToolBar(this)),
    m_bottomPanel(new DockStack(SideButtonPanel::North, this)),
    m_leftPanel(new DockStack(SideButtonPanel::West, this)),
    m_rightPanel(new DockStack(SideButtonPanel::East, this))
{
    //Set main menu.
    setContentsMargins(0, 0, 0, 0);
    setMenuBar(m_mainMenu);
    //Add widget to container.
    QWidget *container = new QWidget(this);
    setCentralWidget(container);
    // Add layout for the widget.
    QBoxLayout *toolbarLayout = new QBoxLayout(QBoxLayout::TopToBottom,
                                               container);
    toolbarLayout->setContentsMargins(0, 0, 0, 0);
    toolbarLayout->setSpacing(0);
    container->setLayout(toolbarLayout);
    //Add toolbar to layout.
    toolbarLayout->addWidget(m_mainToolBar);
    //Create the main container.
    QWidget *panelContainer = new QWidget(this);
    toolbarLayout->addWidget(panelContainer, 1);
    QBoxLayout *bottomLayout = new QBoxLayout(QBoxLayout::TopToBottom,
                                              panelContainer);
    bottomLayout->setContentsMargins(0, 0, 0, 0);
    bottomLayout->setSpacing(0);
    panelContainer->setLayout(bottomLayout);
    //Create the side panel layout.
    QBoxLayout *sideLayout = new QBoxLayout(QBoxLayout::LeftToRight);
    sideLayout->addWidget(m_leftPanel->sidePanel());
    //Add widget.
    QSplitter *topDownSplitter = new QSplitter(Qt::Vertical, panelContainer);
    topDownSplitter->setHandleWidth(0);
    QSplitter *leftRightSplitter = new QSplitter(Qt::Horizontal, topDownSplitter);
    leftRightSplitter->setHandleWidth(0);
    leftRightSplitter->addWidget(m_leftPanel);
    leftRightSplitter->addWidget(m_workspace);
    leftRightSplitter->addWidget(m_rightPanel);
    topDownSplitter->setStretchFactor(1, 1);
    topDownSplitter->addWidget(leftRightSplitter);
    topDownSplitter->addWidget(m_bottomPanel);
    topDownSplitter->setStretchFactor(0, 1);
    sideLayout->addWidget(topDownSplitter);
    sideLayout->addWidget(m_rightPanel->sidePanel());
    bottomLayout->addLayout(sideLayout, 1);
    //Add panel to layout.
    bottomLayout->addWidget(m_bottomPanel->sidePanel());
}
